﻿@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard>
    <DataTableCard TModel="TagOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" Title="@($"{_name}列表")" ShowAddButton="false" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <div style="width: 170px;">
                    @if (context.Item.IsHidden)
                    {
                        <MIcon Color="success"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,false)">@IconType.Check.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,true)">@IconType.Close.ToIconString()</MIcon>
                    }

                </div>
            }
            else if (context.Header.Value == nameof(TagOverviewModel.IsHidden))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Recommends.StateChip IsHidden="context.Item.IsHidden" />
            }
            else if (context.Header.Value == nameof(TagOverviewModel.Name))
            {
                <TagChip Id="@context.Item.Id" Name="@context.Item.Name" />
            }
            else if (context.Header.Value == nameof(TagOverviewModel.LastEditTime))
            {
                @context.Item.LastEditTime.ToString("yyyy-MM-dd HH:mm")
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>

@code {
    private string _baseUrl = "api/tags/List";
    private string _name = "标签";

    private List<DataTableHeader<TagOverviewModel>> _headers = new List<DataTableHeader<TagOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(TagOverviewModel.Id)
          },
          new (){ Text= "名称", Value= nameof(TagOverviewModel.Name)},
          new (){ Text= "最后编辑时间", Value= nameof(TagOverviewModel.LastEditTime)},
          new (){ Text= "状态", Value= nameof(TagOverviewModel.IsHidden)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<TagOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    public async Task HideItem(TagOverviewModel model, bool isHidden)
    {
        string operationName = (isHidden ? "隐藏" : "显示") + _name;
        try
        {
            var obj = await _httpService.PostAsync<HiddenTagModel, Result>("api/Tags/HiddenTag", new HiddenTagModel { Ids = new int[]{model.Id}, IsHidden = isHidden });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
}
